import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    WebViewState state, Dispatch dispatch, ViewService viewService) {
  if (state.title == null)
    return Material(
      color: ThemeConfig.mainColor,
      child: SafeArea(
        child: Container(
          color: Color.fromRGBO(244, 245, 245, 1.0),
          child: SizedBox.expand(
            child: WillPopScope(
              onWillPop: () async {
                dispatch(
                  WebViewActionCreator.onBack(),
                );
                return false;
              },
              child: Stack(
                children: <Widget>[
                  Offstage(
                    offstage: !state.isLoad,
                    child: viewService.buildComponent('WebView'),
                  ),
                  Offstage(
                    offstage: state.isLoad,
                    child: SpinKitCircle(color: ThemeConfig.mainColor),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  else {
    return Scaffold(
      backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
      appBar: myAppBar(viewService.context,title: state.title),
      body: WillPopScope(
        onWillPop: () async {
          dispatch(
            WebViewActionCreator.onBack(),
          );
          return false;
        },
        child: Stack(
          children: <Widget>[
            Offstage(
              offstage: !state.isLoad,
              child: viewService.buildComponent('WebView'),
            ),
            Offstage(
              offstage: state.isLoad,
              child: SpinKitCircle(color: ThemeConfig.mainColor),
            )
          ],
        ),
      ),
    );
  }
}
